﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChoViet.Model;
using ChoViet.Lib.Common;
using System.Linq.Dynamic;
namespace ChoViet.Site.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        ChoVietDataContext db = new ChoVietDataContext();
        /// <summary>
        /// Only apply for menu has leve 3
        /// </summary>
        /// <returns></returns>
        /*
        public ActionResult GetLeft()
        {
            var url = Request.Url.PathAndQuery;
            var m = db.Menus.Where(e => (e.PubYN == true) && (e.NavigateUrl == url) && (e.TypeID == MenuListType.Top)).SingleOrDefault();
            if (m != null && m.MenuMaps.Count > 0)
            {
                var l = db.Menus.Where(e =>(e.PubYN == true)&&( e.ParentID == m.MenuMaps[0].LinkToID)).OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
                return View(l);
            }
            else {
                var ml = db.Menus.Where(e => (e.PubYN == true) && (e.NavigateUrl == url) && (e.TypeID == MenuListType.Left)).SingleOrDefault();
                
                var parentid = ml.ParentMenu == null ? ml.ID : (ml.ParentMenu.ParentMenu == null ? ml.ParentMenu.ID : ml.ParentMenu.ParentID);
                var l = db.Menus.Where(e => (e.PubYN == true) && (e.ParentID == parentid)) 
                    .OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
                return View(l); 
            }           
        }
         */ 
        public ActionResult GetTop()
        {
            try
            {
                var l = db.Menus.Where(e => (e.PubYN == true) && (e.ParentID == null) && (e.TypeID == MenuListType.Top)).OrderBy(e => e.Priority).ToList();
                return View(l);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }
        
    }
}
